package com.iniyo.monitor.client.views;

import com.iniyo.monitor.client.service.HttpService;
import com.iniyo.monitor.client.service.HttpServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;

public class LeftView extends JPanel {

    private HttpService httpService;
    private JButton httpButton;
    private JTextField urlText;
    private JTextField numberText;

    public LeftView() {
        httpService = new HttpServiceImpl();

        JPanel jPanel = new JPanel(new BorderLayout());
        httpButton = new JButton("开始");
        // 添加按钮的点击事件监听器
        httpButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                startHttp();
            }
        });
        jPanel.add(httpButton, BorderLayout.WEST);
        numberText = new JTextField(6);
        numberText.setText("并发次数");
        jPanel.add(numberText, BorderLayout.NORTH);
        urlText = new JTextField(40);
        urlText.setText("请填写地址");
        jPanel.add(urlText);
        add(jPanel, BorderLayout.EAST);
        
        numberText.dispatchEvent(new FocusEvent(numberText, FocusEvent.FOCUS_GAINED, true));
        numberText.requestFocusInWindow();
    }

    private void startHttp() {
        String url = urlText.getText();
        String numberStr = numberText.getText();
        int number = Integer.parseInt(numberStr);
//        String url = "http://10.3.134.240/";
//        int number = 100;
        //开始发起并发请求
        httpService.concurrentHttp(url, number);
    }

}
